package com.yhd.gmall.product.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yhd.gmall.common.result.Result;
import com.yhd.gmall.model.product.*;
import com.yhd.gmall.product.service.ProductService;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author yhd
 * @createtime 2020/9/21 19:18
 */
@RestController
@Slf4j
@RequestMapping("/admin/product")
public class ProductController {

    @Autowired
    private ProductService productService;

    /**
     * 获取一级分类
     * http://api.gmall.com/admin/product/getCategory1
     */
    @GetMapping("getCategory1")
    public Result<List<BaseCategory1>> getBaseCategory1() {
        return Result.ok(productService.getBaseCategory1());
    }

    /**
     * 获取二级分类ID
     * http://api.gmall.com/admin/product/getCategory2/{category1Id}
     */
    @GetMapping("getCategory2/{category1Id}")
    public Result<List<BaseCategory2>> getBaseCategory2(@PathVariable("category1Id") Long category1Id) {
        return Result.ok(productService.getBaseCategory2(category1Id));
    }

    /**
     * 获取三级分类ID
     * http://api.gmall.com/admin/product/getCategory3/{category2Id}
     */
    @GetMapping("getCategory3/{category2Id}")
    public Result<List<BaseCategory3>> getBaseCategory3(@PathVariable("category2Id") Long category2Id) {
        return Result.ok(productService.getBaseCategory3(category2Id));
    }

    /**
     * 根据分类ID获取平台属性
     * http://api.gmall.com/admin/product/attrInfoList/{category1Id}/{category2Id}/{category3Id}
     */
    @GetMapping("attrInfoList/{category1Id}/{category2Id}/{category3Id}")
    public Result<List<BaseAttrInfo>> getAttrInfoList(
            @PathVariable("category1Id") Long category1Id,
            @PathVariable("category2Id") Long category2Id,
            @PathVariable("category3Id") Long category3Id
    ) {
        return Result.ok(productService.getBaseAttrInfo(category1Id, category2Id, category3Id));
    }

    /**
     * 添加/修改平台属性
     * http://api.gmall.com/admin/product/saveAttrInfo
     */
    @PostMapping("saveAttrInfo")
    public Result<Void> saveAttrInfo(@RequestBody BaseAttrInfo baseAttrInfo) {
        productService.save(baseAttrInfo);
        return Result.ok();
    }

    /**
     * 根据平台属性ID获取平台属性
     * http://api.gmall.com/admin/product/getAttrValueList/{attrId}
     */
    @GetMapping("getAttrValueList/{attrId}")
    public Result<List<BaseAttrValue>> getAttrValueList(@PathVariable("attrId") Long attrId) {
        BaseAttrInfo baseAttrInfo = productService.getAttrInfo(attrId);
        List<BaseAttrValue> baseAttrValueList = baseAttrInfo.getAttrValueList();
        return Result.ok(baseAttrValueList);
    }


    /**
     * 获取spu分页列表
     * http://api.gmall.com/admin/product/ {page}/{limit}?category3Id=61
     */
    @GetMapping("{page}/{size}")
    public Result<IPage<SpuInfo>> selectSpuInfoPage(@PathVariable Long page,
                                                    @PathVariable Long size,
                                                    SpuInfo spuInfo) {
        Page<SpuInfo> pageParam = new Page<>(page, size);
        return Result.ok(productService.selectPage(pageParam, spuInfo));
    }


    /**
     * 分页查询品牌信息
     */
    @GetMapping("baseTrademark/{page}/{limit}")
    public Result<IPage<BaseTrademark>> getBaseTrademarkList(@PathVariable Long page,
                                                             @PathVariable Long limit) {
        Page<BaseTrademark> pageParam = new Page<>(page, limit);
        return Result.ok(productService.selectPage(pageParam));
    }

    /**
     * 获取品牌信息
     */
    @GetMapping("baseTrademark/get/{id}")
    public Result<BaseTrademark> getBaseTrademark(@PathVariable Long id) {
        return Result.ok(productService.getBaseTrademark(id));
    }

    /**
     * 品牌添加
     */
    @ApiOperation(value = "新增BaseTrademark")
    @PostMapping("baseTrademark/save")
    public Result save(@RequestBody BaseTrademark banner) {
        productService.saveBaseTrademark(banner);
        return Result.ok();
    }

    /**
     * 品牌删除
     */
    @ApiOperation(value = "删除BaseTrademark")
    @DeleteMapping("baseTrademark/remove/{id}")
    public Result remove(@PathVariable Long id) {
        productService.deleteBaseTrademark(id);
        return Result.ok();
    }

    /**
     * 品牌修改
     */
    @ApiOperation(value = "修改BaseTrademark")
    @PutMapping("baseTrademark/update")
    public Result updateById(@RequestBody BaseTrademark banner) {
        productService.updateBaseTrademark(banner);
        return Result.ok();
    }

    /**
     * 查询全部品牌
     *
     * @return
     */
    @GetMapping("baseTrademark/getTrademarkList")
    public Result<List<BaseTrademark>> getTrademarkList() {
        List<BaseTrademark> baseTrademarkList = productService.getTrademarkList();
        return Result.ok(baseTrademarkList);
    }

    /**
     * 获取销售属性值集合
     *
     * @return
     */
    @GetMapping("baseSaleAttrList")
    public Result baseSaleAttrList() {
        // 查询所有的销售属性集合
        List<BaseSaleAttr> baseSaleAttrList = productService.getBaseSaleAttrList();
        return Result.ok(baseSaleAttrList);
    }

    /**
     * 保存spu
     *
     * @param spuInfo
     * @return
     */
    @PostMapping("saveSpuInfo")
    public Result saveSpuInfo(@RequestBody SpuInfo spuInfo) {
        productService.saveSpuInfo(spuInfo);
        return Result.ok();
    }

    /**
     * 根据spuid查询所有的imageList
     */
    @GetMapping("spuImageList/{spuId}")
    public Result<List<SpuImage>> getSpuImage(@PathVariable("spuId") Long spuId) {
        return Result.ok(productService.getSpuImageList(spuId));
    }

    /**
     * 根据spuid查询销售属性值集合
     */
    @GetMapping("spuSaleAttrList/{spuId}")
    public Result<List<SpuSaleAttr>> getSpuSaleAttrList(@PathVariable("spuId") Long spuId) {
        return Result.ok(productService.getSpuSaleAttrList(spuId));
    }

    /**
     * sku保存
     */
    @PostMapping("saveSkuInfo")
    public Result saveSkuInfo(@RequestBody SkuInfo skuInfo) {
        productService.saveSkuInfo(skuInfo);
        return Result.ok();
    }

    /**
     * sku分页列表
     */
    @GetMapping("/list/{page}/{limit}")
    public Result index(
            @PathVariable Long page,
            @PathVariable Long limit) {

        Page<SkuInfo> pageParam = new Page<>(page, limit);
        IPage<SkuInfo> pageModel = productService.selectPages(pageParam);
        return Result.ok(pageModel);
    }

    /**
     * 商品上架
     */
    @GetMapping("onSale/{skuId}")
    public Result onSale(@PathVariable("skuId") Long skuId) {
        productService.onSale(skuId);
        return Result.ok();
    }

    /**
     * 商品下架
     */
    @GetMapping("cancelSale/{skuId}")
    public Result cancelSale(@PathVariable("skuId") Long skuId) {
        productService.canceSale(skuId);
        return Result.ok();
    }
}
